package com.dc.project.controller.user;

import com.dc.project.constant.ControllerConstant;
import com.dc.project.service.TaskResultGenerateService;
import com.own.aop.login.LoginIgnore;
import com.own.aop.operation.LogOperationModuleAop;
import com.own.business.resource.entity.vo.FileRecordVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.store.core.util.LockStoreUtil;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * UserClientTaskResultGenerateController
 *
 * @author chenxueli
 * @date 2025-03-11 16:24:48
 */
@Slf4j
@Tag(name = "任务最终材料生成记录" + ConstantBeanSuffix.CONTROLLER)
@RestController
@LoginIgnore
@CrossOrigin("*")
@LogOperationModuleAop("任务最终材料生成记录")
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ControllerConstant.PREFIX_USER_API + "task/result/generate")
public class UserClientTaskResultGenerateController extends BaseController {

    @Resource
    private TaskResultGenerateService taskResultGenerateService;

    @Resource
    private LockStoreUtil lockStoreUtil;

    @Operation(summary = "根据item生成")
    @GetMapping("/by/item/{itemId}")
    public ResultModel<Boolean> generateByItem(
            @Parameter(description = "项目id", required = true) @PathVariable Long itemId
    ) {
        taskResultGenerateService.generateByItemId(itemId);
        return R.success();
    }

    @Operation(summary = "根据detail生成")
    @GetMapping("/by/detail/{detailId}")
    public ResultModel<Boolean> generateByDetail(
            @Parameter(description = "详情id", required = true) @PathVariable Long detailId
    ) {
        taskResultGenerateService.generateByDetailId(detailId);
        return R.success();
    }

    @Operation(summary = "根据task生成")
    @GetMapping("/by/task/{taskId}")
    public ResultModel<Boolean> generateByTask(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId
    ) {
        taskResultGenerateService.generateByTaskId(taskId);
        return R.success();
    }

    @Operation(summary = "根据item打包下载")
    @GetMapping("/package/by/item/{itemId}")
    public ResultModel<FileRecordVo> packageByItem(
            @Parameter(description = "项目id", required = true) @PathVariable Long itemId
    ) {
        var vo = lockStoreUtil.lock("package:item:" + itemId, () -> taskResultGenerateService.packageByItemId(itemId));
        return R.success(vo);
    }

    @Operation(summary = "根据detail打包下载")
    @GetMapping("/package/by/detail/{detailId}")
    public ResultModel<FileRecordVo> packageByDetail(
            @Parameter(description = "详情id", required = true) @PathVariable Long detailId
    ) {
        var vo = lockStoreUtil.lock("package:detail:" + detailId, () -> taskResultGenerateService.packageByDetailId(detailId));
        return R.success(vo);
    }

    @Operation(summary = "根据task打包下载")
    @GetMapping("/package/by/task/{taskId}")
    public ResultModel<FileRecordVo> packageByTask(
            @Parameter(description = "任务id", required = true) @PathVariable Long taskId
    ) {
        var vo = lockStoreUtil.lock("package:task:" + taskId, () -> taskResultGenerateService.packageByTaskId(taskId));
        return R.success(vo);
    }

}
